package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myapplication.R;
import com.example.myapplication.bean.Product;

import java.util.List;

public class CartAdapter extends ArrayAdapter<Product> {

    public CartAdapter(@NonNull Context context, int resource, @NonNull List<Product> objects) {
        super(context, resource, objects);

    }

    @NonNull
    @Override
    // 返回每一行的显示内容
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        Product product = getItem(position);
        // 加载布局文件，生成一个view
        View view = LayoutInflater.from(getContext()).inflate(R.layout.cart_listview_item, parent, false);

        ImageView imageView = view.findViewById(R.id.image);   // 查找图片组件
        imageView.setImageResource(product.getImage());
        TextView textView = view.findViewById(R.id.name);     // 查找商品名字对应TextView组件
        textView.setText(product.getName());

        TextView priceView = view.findViewById(R.id.price);
        priceView.setText("￥" + product.getPrice());

        TextView typeView = view.findViewById(R.id.type);
        if (product.getType() == 0) {
            typeView.setVisibility(View.INVISIBLE);
        } else {
            typeView.setText("自营");
        }
        return view;
    }
}
